import path from 'path-browserify'

export function resolveRoutePath(basePath?: string, routePath?: string) {
  return basePath ? path.resolve(basePath, routePath ?? '') : routePath ?? ''
}

// 反转父子级数据
export function reverseData(data: any) {
  if (!data) {
    return data
  }
  let current = data
  if (data.Parent) {
    let parent = { ...data.Parent }
    while (parent) {
      parent.Children = [{ ...current }]
      current = parent
      parent = parent.Parent
      delete current.Parent
    }
  }
  return current
}

export function getColor() {
  const generateRandomNumber = () => {
    const min = 50 // 最小值
    const max = 200 // 最大值
    return Math.floor(Math.random() * (max - min + 1)) + min
  }
  const r = generateRandomNumber()
  const g = generateRandomNumber()
  const b = generateRandomNumber()

  // 将RGB分量转换为十六进制字符串，并确保生成的字符串始终为两位
  const rHex = r.toString(16).padStart(2, '0')
  const gHex = g.toString(16).padStart(2, '0')
  const bHex = b.toString(16).padStart(2, '0')

  // 使用生成的RGB值创建颜色字符串，例如"#aabbcc"
  const color = `#${rHex}${gHex}${bHex}`

  return color
}

// 更新url
export function repaceUrl(url: string, params?: any) {
  const origin = window.location.origin
  const hash = window.location.hash
  const search = params ? new window.URLSearchParams(params) : ''
  const path = `${origin}${hash ? `/#` : ''}${url}${search ? `?${search.toString()}` : ''}`
  history.replaceState(null, '', path)
}
